<template>
  <!-- 注册页面 -->
  <div class="container-reg">
    <div class="box-reg">
      <div class="box-reg-title"></div>
      <!-- //表单 -->
      <!-- 第二步,给el-form组件添加ref值 -->
      <el-form :model="regForm" :rules="regFormRules" ref="regRef">
        <!-- //用户名 字体图标,提示 -->
        <el-form-item prop="username">
          <el-input clearable placeholder="用户名" prefix-icon="el-icon-user" v-model="regForm.username"></el-input>
        </el-form-item>
        <!-- //密码 -->
        <el-form-item prop="password">
          <el-input
            placeholder="密码"
            prefix-icon="el-icon-lock"
            show-password
            v-model="regForm.password"
            clearable
          ></el-input>
        </el-form-item>
        <!-- 再次确定密码 -->
        <el-form-item prop="repassword">
          <el-input
            clearable
            placeholder="再次确定密码"
            prefix-icon="el-icon-lock"
            show-password
            v-model="regForm.repassword"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <!-- //注册 -->
          <!-- 全局表单验证 1.添加点击事件1-->
          <el-button type="primary" class="zc-all" @click="regall">注册</el-button>
          <el-link type="primary" @click="tologin">去登录</el-link>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { regAPI } from '@/api/uers.js'
export default {
  name: 'reg',
  data() {
    const checkall = (rule, value, cb) => {
      if (value === this.regForm.password) {
        cb()
      } else {
        cb(new Error('两次密码不一致'))
      }
    }
    return {
      regForm: {
        username: '',
        password: '',
        repassword: ''
      },
      regFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,10}$/, message: '要求字母数字、长度 1-10', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '要求非空字符串、长度 6-15', trigger: 'blur' }
        ],
        repassword: [
          // 必填项验证
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          // 正则验证
          { pattern: /^\S{6,15}$/, message: '要求非空字符串、长度 6-15', trigger: 'blur' },
          // 自定义验证
          { validator: checkall, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    regall() {
      // 第三步,先全局验证,再ajax请求
      // 通过element,form组件方法
      this.$refs.regRef.validate(async valid => {
        // 表示全局验证,valid表示是否通过
        if (!valid) return
        // ajax请求
        const { data: res } = await regAPI(this.regForm)
        if (res.code === 1) {
          this.$message.error(res.message)
        } else {
          this.$message.success(res.message)
          this.$router.push({
            path: '/login'
          })
        }
      })
    },
    tologin() {
      this.$router.push({
        name: 'login'
      })
    }
  }
}
</script>

<style lang="less" scoped>
.container-reg {
  height: 100%;
  background: url('./../../assets/images/login_bg.jpg'), no-repeat;
  background-size: cover;
  .box-reg {
    width: 400px;
    height: 350px;
    padding: 0px 20px;
    box-sizing: border-box;
    background: white;
    border-radius: 20px;
    position: absolute;
    top: 50%;
    left: 50%;
    margin-top: -200px;
    margin-left: -167.5px;
    .box-reg-title {
      height: 80px;
      background: url('./../../assets/images/login_title.png') no-repeat;
      background-position: center;
    }
    .zc-all {
      width: 100%;
    }
  }
}
</style>
